package com.jie.common.http.entity.response;

import com.jie.common.http.constant.HttpCharacterConstant;
import com.jie.common.http.constant.HttpVersionConstant;
import com.jie.common.http.constant.response.HttpResponseStatusConstant;

import java.nio.charset.StandardCharsets;

/**
 * 响应行
 * @author XieZhiJie
 * @date 2022/04/21 17:24
 */
public class HttpResponseLine {

    /**
     * 默认HTTP/1.1
     */
    private String httpVersion;

    /**
     * 状态码
     */
    private String statusCode;

    /**
     * 状态信息
     */
    private String statusDescription;

    public HttpResponseLine() {
        this(HttpResponseStatusConstant.SUCCESS.statusCode, HttpResponseStatusConstant.SUCCESS.statusDescription);
    }

    public HttpResponseLine(String statusCode, String statusDescription) {
        this(HttpVersionConstant.HTTP_VERSION_1_1, statusCode, statusDescription);
    }

    public HttpResponseLine(String httpVersion, String statusCode, String statusDescription) {
        this.httpVersion = httpVersion;
        this.statusCode = statusCode;
        this.statusDescription = statusDescription;
    }

    public String getStatusCode() {
        return this.statusCode;
    }

    public void setStatusCode(String statusCode) {
        this.statusCode = statusCode;
    }

    public String getStatusDescription() {
        return this.statusDescription;
    }

    public void setStatusDescription(String statusDescription) {
        this.statusDescription = statusDescription;
    }

    /**
     * 将当前对象的数据转为字节数组
     * @return 返回一个字节数组
     */
    public byte[] getBytes() {
        // 虽然这里用UTF-8, 但是因为响应行都是全英, 又因为UTF8的特性,
        // 最后转化的byte数组全都是ascii码
        return toString().getBytes(StandardCharsets.UTF_8);
    }

    /**
     * 从bytes数组中取出对应数据封装到响应行中
     * @param bytes      字节数组
     * @param bytesIndex 字节数组开始索引
     * @return           返回取完行数据后的字节数组结束索引
     */
    public int addHeaderLine(byte[] bytes, int bytesIndex) {
        char eof = HttpCharacterConstant.LINE_FEED;
        char sep = HttpCharacterConstant.HEADER_LINE_SEPARATE;
        byte b = bytes[bytesIndex];
        while (b != eof) {
            int startIndex = bytesIndex;
            while (b != sep) {
                b = bytes[++bytesIndex];
            }
            this.httpVersion = new String(bytes, startIndex, bytesIndex - startIndex);
            b = bytes[++bytesIndex];
            startIndex = bytesIndex;
            while (b != sep) {
                b = bytes[++bytesIndex];
            }
            this.statusCode = new String(bytes, startIndex, bytesIndex - startIndex);
            b = bytes[++bytesIndex];
            startIndex = bytesIndex;
            while (b != eof) {
                b = bytes[++bytesIndex];
            }
            this.statusDescription = new String(bytes, startIndex, bytesIndex - startIndex);
        }
        return bytesIndex;
    }

    @Override
    public String toString() {
        return this.httpVersion + " " + this.statusCode + " " + this.statusDescription + "\r\n";
    }

}
